﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_QuestionManager_CombinationQuestion_Edit_pand : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerQuestion objQuestion = new ManagerQuestion();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string QuestionID = Base64.Decode(Request.QueryString["qid"].ToString());
            string FatherQuestionID = Base64.Decode(Request.QueryString["fthID"].ToString());
            ViewState["QuestionID"] = QuestionID;
            ViewState["FatherQuestionID"] = FatherQuestionID;
            ViewState["TypeID"] = 1;
            
            Pageload();
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionCount.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionOption.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_RightAnswer.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_RightAnswer", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void Pageload()
    {
        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString()));
        ViewState["objQuestionInfo"] = objQuestionInfo;



        txtQuestionContent.Value = objQuestionInfo.QuestionContent;


        #region 绑定选项
        ListItem objlist;
        for (int i = 0; i < 2; i++)
        {
            objlist = new ListItem();
            switch (i)
            {
                case 0: objlist.Text = "A";
                    //objlist.Selected = true;
                    break;
                case 1: objlist.Text = "B";
                    break;

            }
            objlist.Value = i.ToString();

            rdbtn_answer.Items.Add(objlist);
        }
        if (objQuestionInfo.QuestionAnswer == "A")
        {
            rdbtn_answer.Items[0].Selected = true;
        }
        else
        {
            rdbtn_answer.Items[1].Selected = true;
        }
        #endregion

        #region 绑定选项内容
        DataTable dt_QuestionOption = new DataTable();
        string search = "FKQuestionOptionQuestionID=" + ViewState["QuestionID"].ToString();
        dt_QuestionOption = objQuestion.QueryQuestionOption(search);
        ViewState["dt_QuestionOption"] = dt_QuestionOption;

        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            if (Option == "A")
            {
                txtOptionA.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else
            {
                txtOptionB.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }

        }
        #endregion

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = (VOQuestion)ViewState["objQuestionInfo"];
        //objQuestionInfo.FKQuestionCourseID = int.Parse(ViewState["FKQuestionCourseID"].ToString());
        //objQuestionInfo.QuestionCreateID = int.Parse(Session["UserID"].ToString());
        //info.DateCreated = DateTime.Now;

        objQuestionInfo.QuestionAnswer = rdbtn_answer.SelectedItem.Text;
        //objQuestionInfo.QuestionDefaultScore = int.Parse(ViewState["QuestionDefaultScore"].ToString());
        //objQuestionInfo.QuestionDiffculty = int.Parse(ViewState["QuestionDiffculty"].ToString());
        //objQuestionInfo.QuestionKen = ViewState["QuestionKen"].ToString();


        objQuestionInfo.QuestionContent = txtQuestionContent.Value;


        //objQuestionInfo.QuestionState = int.Parse(ViewState["QuestionState"].ToString());   //禁用1,启用2


        if (string.IsNullOrEmpty(txtOptionA.Text.Trim()))       //临时使用这种方式，有很多问题，在外面修改时需要注意，至少应该有四项选择
        {
            Script.Alert(Page, "msg", "A" + GetLanageValueByKey("msg_AddQuestionInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        if (string.IsNullOrEmpty(txtOptionB.Text.Trim()))
        {
            Script.Alert(Page, "msg", "B" + GetLanageValueByKey("msg_AddQuestionInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        VOQuestionOption objQuestionOptionInfo = new VOQuestionOption();
        DataTable dt_QuestionOption = (DataTable)ViewState["dt_QuestionOption"];
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            string optionID = dt_QuestionOption.Rows[i]["QuestionOptionID"].ToString();
            objQuestionOptionInfo = objQuestion.LoadQuestionOpgion(int.Parse(optionID));
            if (Option == "A")
            {

                objQuestionOptionInfo.QuestionOptionContent = "A、" + txtOptionA.Text;
            }
            else
            {
                objQuestionOptionInfo.QuestionOptionContent = "B、" + txtOptionB.Text;
            }
            objQuestion.UpdateQuetionOption(objQuestionOptionInfo);

        }
        if (objQuestion.UpdateQuetion(objQuestionInfo))//更新试题信息
        {
            Script.ShowAndHistory(GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page), -2);
        }
        else
        {
            Script.Alert(GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("EditQuestionInfo_zuhe.aspx?qid=" + ViewState["FatherQuestionID"].ToString());
    }
}
